import aiohttp
from aiohttp import web


# 获取 module 详情
from data.model import model


async def test(request):
    # put或者post方式参数获取
    # data = await request.json()
    # name = data["name"]
    # print('请求的信息data为: %s' % str(data))

    # /api/{name}/test路径中的值获取方式
    name = request.match_info['name']

    # /api/test?name=joe 参数获取方式
    # query = request.query
    # name = query.get("name")
    print(name)
    try:
        return web.json_response({'code': 0, 'data': name})
    except Exception as e:
        return web.json_response({'msg': e.value}, status=500)


async def get(request):
    # put或者post方式参数获取
    # data = await request.json()
    # name = data["name"]
    # print('请求的信息data为: %s' % str(data))

    # /api/{name}/get路径中的值获取方式
    # name = request.match_info['name']

    # /api/test?name=joe 参数获取方式
    # query = request.query
    # name = query.get("name")
    # print(name)
    url = "http://tp5.com/index/index/getjson"
    params = {'name': 'germey', 'age': 25}
    async with aiohttp.ClientSession() as session:
        async with session.get(url, params=params) as response:
            data = await response.text()
            print(data)
    try:
        return web.json_response({'code': 0, 'data': data})
    except Exception as e:
        return web.json_response({'msg': e.value}, status=500)


# 获取 module 详情
async def get_url(request):
    name = request.match_info['name']
    print(name)
    t = model()
    t.get_name()
    try:
        return web.json_response({'code': 0, 'data': name})
    except Exception as e:
        return web.json_response({'msg': e.value}, status=500)
